package com.uetoken.cn.wxapi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.uetoken.cn.R;
import com.uetoken.cn.common.Constant;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        Log.d(TAG, "onCreate()");
        api = WXAPIFactory.createWXAPI(this, Constant.appId);
        api.handleIntent(getIntent(), this);
//        api.isWXAppInstalled()
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        Log.d(TAG, "onPayFinish onReq, getType = " + req.getType());
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.d(TAG, "onPayFinish, getType = " + resp.getType());
        Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
        switch (resp.errCode) {
            case 0:
//                if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
//                    builder.setTitle("提示");
//                    builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
//                        @Override
//                        public void onCancel(DialogInterface dialog) {
//                            finish();
//                        }
//                    });
//                    builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//                    builder.show();
//                }
                ToastUtils.showShort(R.string.str_pay_success);
                finish();
                break;
//            case -1:
//                Toast.makeText(WXPayEntryActivity.this, "支付错误", Toast.LENGTH_SHORT).show();
//                finish();
//                break;
//            case -2:
//                ToastUtils.showShort(R.string.str_pay_fail);
//                finish();
//                break;
            default:
                ToastUtils.showShort(R.string.str_pay_fail);
                finish();
                break;
        }
    }
}